package com.dz.Incubator.controller;

import com.dz.Incubator.config.Pagination;
import com.dz.Incubator.config.ReturnCode;
import com.dz.Incubator.entity.BusinessSchoolEntity;
import com.dz.Incubator.entity.InvokeResult;
import com.dz.Incubator.service.BusinessSchoolService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;

import javax.validation.Valid;
import java.util.List;


@RestController
@RequestMapping("/api/businessSchool")
@Api(tags = {"商学院管理"})
@Validated
public class BusinessSchoolController {

	private static final Logger logger = LoggerFactory.getLogger(BusinessSchoolController.class);

	@Autowired
	private BusinessSchoolService businessSchoolService;
    
	/**
	 * 集合查找 BusinessSchoolEntity 对象
	 *
	 * @param ids 集合查找数据
	 */
	@PostMapping(value = "/find")
	@ApiOperation("根据ID批量查找")
	public InvokeResult<List<BusinessSchoolEntity>> find(@RequestBody Long[] ids) {
		InvokeResult invokeResult = null;
		try {
			if (null == ids && ids.length <= 0) {
				return invokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
			} else {
				invokeResult = InvokeResult.success(businessSchoolService.find(ids));
			}
		} catch (Exception e) {
			invokeResult = InvokeResult.error();
			logger.error(e.getMessage(), e);
		}
		return invokeResult;
    }
	
	/**
	 * 分页查询 businessSchool 对象
	 *
	 * @param pagination 条件
	 */
	@GetMapping(value = "/findByPageList")
	@ApiOperation("分页查询")
	public InvokeResult<Pagination> getPageList(Pagination pagination) {
		InvokeResult invokeResult = null;
		try {
			if (null == pagination) {
				return invokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
			}
			invokeResult = InvokeResult.success(businessSchoolService.findByPagination(pagination));
		} catch (Exception e) {
            invokeResult = InvokeResult.error();
            logger.error(e.getMessage(), e);
        }
		return invokeResult;
	}

    
    /**
	 * 查找所有对象
	 *
	 */
	@GetMapping(value = "/findAll")
	@ApiOperation("查询所有")
	public InvokeResult<List<BusinessSchoolEntity>> findAll() {
		InvokeResult invokeResult = null;
		try {
			invokeResult = InvokeResult.success(businessSchoolService.findAll());
		} catch (Exception e) {
			invokeResult = InvokeResult.error();
			logger.error(e.getMessage(), e);
		}
		return invokeResult;
    }

    /**
     * 新增数据
     *
     * @param businessSchoolEntity 新增对象
     */
    @PostMapping(value = "/insert")
    @ApiOperation("新增数据")
    public InvokeResult<BusinessSchoolEntity> insert(@Valid @RequestBody BusinessSchoolEntity businessSchoolEntity) {
        InvokeResult invokeResult = null;
        try {
			if (null == businessSchoolEntity) {
				return invokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
			} else {
				invokeResult = InvokeResult.success(businessSchoolService.insert(businessSchoolEntity));
			}
        } catch (Exception e) {
			invokeResult = InvokeResult.error();
			logger.error(e.getMessage(), e);
        }
		return invokeResult;
    }

	/**
	 * 修改数据
	 *
	 * @param businessSchoolEntity 修改对象
	 */
	@PostMapping(value = "/update")
	@ApiOperation("修改数据")
	public InvokeResult<BusinessSchoolEntity> update(@Valid @RequestBody BusinessSchoolEntity businessSchoolEntity) {
		InvokeResult invokeResult = null;
		try {
			if (null == businessSchoolEntity) {
				return invokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
			} else {
				invokeResult = InvokeResult.success(businessSchoolService.update(businessSchoolEntity));
			}
		} catch (Exception e) {
			invokeResult = InvokeResult.error();
			logger.error(e.getMessage(), e);
		}
		return invokeResult;
	}
	
	/**
	 * 获取单个BusinessSchoolEntity 对象
	 *
	 * @param id Id
	 */
	@GetMapping(value = "get/{id}")
	@ApiOperation("根据Id查询")
	public InvokeResult<BusinessSchoolEntity> get(@PathVariable Long id) {
		InvokeResult invokeResult = null;
		try {
			if (null == id) {
				return invokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
			} else {
				invokeResult = InvokeResult.success(businessSchoolService.get(id));
			}
		} catch (Exception e) {
			invokeResult = InvokeResult.error();
			logger.error(e.getMessage(), e);
		}
		return invokeResult;
    }

	/**
	 * 删除单个BusinessSchoolEntity 对象
	 *
	 * @param id 删除数据 ID
	 */
	@GetMapping(value = "/delete/{id}")
	@ApiOperation("根据Id删除")
	public InvokeResult delete(@PathVariable Long id) {
		InvokeResult invokeResult = null;
		try {
			if (null == id) {
				return invokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
			} else {				
				invokeResult = InvokeResult.success(businessSchoolService.delete(id));
			}
		} catch (Exception e) {
			invokeResult = InvokeResult.error();
			logger.error(e.getMessage(), e);
		}
		return invokeResult;
    }
	
	
	/**
	 * 批量添加数据
	 *
	 * @param list 添加对象列表
	 */
	@PostMapping(value = "/insertBatch")
	@ApiOperation("批量添加")
	public InvokeResult<List<BusinessSchoolEntity>> insertBatch(@Valid @RequestBody List<BusinessSchoolEntity> list) {
		InvokeResult invokeResult = null;
		try {
			if (null == list || list.size() <= 0) {
				return invokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
			} else {
				invokeResult = InvokeResult.success(businessSchoolService.insertBatch(list));
			}
		} catch (Exception e) {
			invokeResult = InvokeResult.error();
			logger.error(e.getMessage(), e);
		}
		return invokeResult;
	}
	
	/**
	 * 批量修改数据
	 *
	 * @param list 修改对象列表
	 */
	@PostMapping(value = "/updateBatch")
	@ApiOperation("批量修改")
	public InvokeResult<List<BusinessSchoolEntity>> updateBatch(@Valid @RequestBody List<BusinessSchoolEntity> list) {
		InvokeResult invokeResult = null;
		try {
			if (null == list || list.size() <= 0) {
				return invokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
			} else {
				invokeResult = InvokeResult.success(businessSchoolService.updateBatch(list));
			}
		} catch (Exception e) {
			invokeResult = InvokeResult.error();
			logger.error(e.getMessage(), e);
		}
		return invokeResult;
	}
	
	/**
	 * 批量删除数据
	 *
	 * @param ids 删除对象ids数组
	 */
	@PostMapping(value = "/delBatch")
	@ApiOperation("批量删除")
	public InvokeResult<List<BusinessSchoolEntity>> delBatch(@RequestBody Long[] ids) {
		InvokeResult invokeResult = null;
		try {
			if (null == ids || ids.length <= 0) {
				return invokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
			} else {
				invokeResult = InvokeResult.success(businessSchoolService.deleteBatch(ids));
			}
		} catch (Exception e) {
			invokeResult = InvokeResult.error();
			logger.error(e.getMessage(), e);
		}
		return invokeResult;
	}

}